package bean;

import java.util.List;

public class TrafficLights implements Runnable{
	private boolean started=false;
	private boolean stop=false;
	private int id;
	public Intersection intersection;
	public Lights[] lights=new Lights[4];
	private List<ControlCircle> ctrlO;
	
	private static int count;
	static{
		count=0;
	}
	public static int lightsCount(){
		return count++;
	}
	public TrafficLights(int id,Intersection is){
		this.setId(id);
		this.setIntersection(is);
		lights[0]=new Lights(1,2);
		lights[1]=new Lights(2,3);
		lights[2]=new Lights(3,0);
		lights[3]=new Lights(0,1);
	}
	public TrafficLights(int id,Intersection is,List<ControlCircle> ctrlO){
		this(id,is);
		this.setCtrlO(ctrlO);
	}
	
	public void setCtrlO(List<ControlCircle> ctrlO){
		this.ctrlO=ctrlO;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Intersection getIntersection() {
		return intersection;
	}

	public void setIntersection(Intersection intersection) {
		this.intersection = intersection;
	}

	/**
	 * 根据路口关键点序号获取对应的红绿灯
	 *      |  | |    |
	 *      |  V | L0 |
	 * -----   0    1  -----
	 *  L3 7           2<---
	 * -----           -----
	 * --->6           3 L1
	 * -----   5    4  -----
	 *      | L2 |  ^ |
	 *      |    |  | |
	 * 数字：0~7标识路口关键点编号
	 * Lx:L标识light，L0表示改路口顺时针第一交通灯
	 * @param index
	 * @return
	 */
	public Lights getLight(int index) {
		Lights l = null;
		switch (index) {
		case 0:
			l = lights[2];
			break;
		case 2:
			l = lights[3];
			break;
		case 4:
			l = lights[0];
			break;
		case 6:
			l = lights[1];
			break;
		}
		return l;
	}

	public void setLights(Lights[] lights) {
		this.lights = lights;
	}
	
	@Override
	public void run() {
		while(!stop){
			for(ControlCircle c:ctrlO){
				control(c);
			}
		}
	}
	public void stop(){
		stop=true;
	}
	
	public void control(ControlCircle cc){
		try {
			control(cc.light,cc.dir,cc.timer);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public void control(Lights light,int dir,int timer) throws InterruptedException{
		Lights opp=lights[light.opposite];
		synchronized (light) {
			switch(dir){
			case Lights.FORWARD:
				light.forward();
				opp.forward();
				break;
			case Lights.LEFT:
				light.left();
				opp.left();
				break;
			}
		}
		Thread.sleep(timer*1000);
		light.lightsOut();
		opp.lightsOut();
	}
	
	/**
	 * 红绿灯开始工作，只允许调用一次
	 */
	public void begin(){
		if(!started){
			new Thread(this).start();
			started=true;
		}
		else
			throw new RuntimeException("can only invoke once!");
	}
	
	public class Lights{
		public static final int LEFT=1,FORWARD=2,RIGHT=3;
		public boolean left=false;
		public boolean forward=false;
		public boolean right=true;
		/**
		 * 下一个亮的灯的数组下标
		 */
		public int next = -1;
		/**
		 * 对面的灯数组下标
		 */
		public int opposite= -1;
		public Lights(int next,int opposite){
			this.next=next;
			this.opposite=opposite;
		}
		public void left(){
			left=true;
			forward=false;
		}
		public void forward(){
			forward=true;
			left=false;
		}
		public void lightsOut(){
			left=false;
			forward=false;
		}
	}
	
	/**
	 * 红绿灯循环控制队列
	 */
	public class ControlCircle{
		public Lights light;
		public int dir;
		public int timer;
		public ControlCircle(Lights l,int dir,int timer){
			this.light=l;
			this.dir=dir;
			this.timer=timer;
		}
	}
}


